package top.swimmer.tools.data.linked;

public class ListNodeMerge {
    public static ListNode listNodeMerge(ListNode first, ListNode second) {
        if (first == null) {
            return second;
        }
        if (second == null) {
            return first;
        }

        ListNode parent = new ListNode(0);
        ListNode prev = parent;
        while (first != null && second != null) {
            if (first.val <= second.val) {
                prev.next = first;
                first = first.next;
            } else {
                prev.next = second;
                second = second.next;
            }
            prev = prev.next;
        }

        prev.next = first == null ? second : first;
        return parent.next;
    }

    public static void main(String[] args) {
        System.out.println(listNodeMerge(ListNode.get(1, 2, 4), ListNode.get(1, 3, 4)));
    }
}
